package cn.nuist.controller;

import cn.nuist.entity.Supplier;
import cn.nuist.service.SupplierService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("supplier")
public class SupplierController {
    private SupplierService supplierService;

    @Autowired
    public SupplierController(SupplierService supplierService) {
        this.supplierService = supplierService;
    }

    @RequestMapping("getAll")
    public String getAll(Model model){
        List<Supplier> allSupplier = supplierService.getAllSupplier();
       // System.out.println("****"+allSupplier);
        model.addAttribute("suppliers",allSupplier);
        return "provider/providerList";
    }

    @RequestMapping("add")
    public String add(Supplier supplier,Model model){
        try {
            supplierService.add(supplier);
        }catch (Exception e){
            model.addAttribute("msg",e.getMessage());
            return "provider/providerAdd";
        }
        return "redirect:/supplier/getAll";
    }

    @RequestMapping("search")
    public String search(String supplierName,HttpSession session) throws UnsupportedEncodingException {
        //System.out.println(supplierName);
        try{
            Supplier supplierByName = supplierService.getSupplierByName(supplierName);
            return "redirect:/supplier/showdetail?supplierName=" +URLEncoder.encode(supplierName,"UTF-8");
        }catch (Exception e){
            session.setAttribute("msg",e.getMessage());
            return "redirect:/supplier/getAll";
        }

    }

    @RequestMapping("showdetail")
    public String show(@RequestParam("supplierName")String supplierName, Model model){
        //System.out.println(" ****" +supplierName);
        Supplier supplier = supplierService.getSupplierByName(supplierName);
        //System.out.println("***"+supplier);
        model.addAttribute("supplier",supplier);
        return "/provider/providerView";
    }


    @RequestMapping("delete")
    public String delete(@RequestParam("supplierID")int supplierID){
        //System.out.println("***" + supplierID);
        supplierService.delete(supplierID);
        return "redirect:/supplier/getAll";
    }

    @RequestMapping("updateView")
    public String updateView(@RequestParam("supplierID")int supplierID,Model model){
        Supplier supplierById = supplierService.getSupplierById(supplierID);
        model.addAttribute("supplier",supplierById);
        return "/provider/providerUpdate";
    }

    @RequestMapping("update")
    public String update(Supplier supplier) throws UnsupportedEncodingException {
        supplierService.update(supplier);
        return "redirect:/supplier/showdetail?supplierName="+URLEncoder.encode(supplier.getSupplierName(),"UTF-8");
    }

}
